import java.util.Random;

/**
 * 加减法口算练习系统 v0.2
 * 新增功能：显示答案、限制结果范围、多列显示
 */
public class CalculationSystemV02 {
    private static Random random = new Random();
    
    /**
     * 生成随机数
     */
    public static int generateRandomNumber(int min, int max) {
        return random.nextInt(max - min + 1) + min;
    }
    
    /**
     * 生成单个算式及答案
     */
    public static String[] generateEquationWithAnswer(int index) {
        int a, b, result;
        String operator;
        String[] equationAndAnswer = new String[2];
        
        boolean isAddition = generateRandomNumber(0, 1) == 1;
        
        if (isAddition) {
            // 加法：确保和不超过100
            do {
                a = generateRandomNumber(0, 100);
                b = generateRandomNumber(0, 100);
                result = a + b;
            } while (result > 100);
            operator = "+";
        } else {
            // 减法：确保差不小于0
            a = generateRandomNumber(0, 100);
            b = generateRandomNumber(0, a); // b不超过a，确保结果非负
            result = a - b;
            operator = "-";
        }
        
        equationAndAnswer[0] = String.format("%2d: %2d %s %2d = %3d", index, a, operator, b, result);
        equationAndAnswer[1] = String.valueOf(result);
        
        return equationAndAnswer;
    }
    
    /**
     * 生成并格式化输出题目（每行显示4个算式）
     */
    public static void generateFormattedExercises() {
        System.out.println("加减法口算练习系统 - v0.2");
        System.out.println("========================");
        System.out.println("每行显示4个算式，包含答案：");
        System.out.println();
        
        for (int i = 1; i <= 50; i += 4) {
            for (int j = 0; j < 4 && (i + j) <= 50; j++) {
                String[] equation = generateEquationWithAnswer(i + j);
                System.out.print(equation[0] + "\t");
            }
            System.out.println();
        }
    }
    
    public static void main(String[] args) {
        generateFormattedExercises();
    }
}